//
//  DBVistorView.swift
//  大波微博swift
//
//  Created by 大波 on 16/2/27.
//  Copyright © 2016年 zhaojin. All rights reserved.
//

import UIKit

protocol DBVistorViewDelegate: NSObjectProtocol {
    
    func loginBtnDidClick()
    
    func registerBtnDidClick()
}

class DBVistorView: UIView {

    //注册登陆按钮代理属性
    weak var vistorViewDelegate: DBVistorViewDelegate?
    
    ///登录按钮回调闭包
    var loginBtnDidClickClosure: (() -> ())?
    
    //注册按钮回调闭包
    var registerDidClickClosure: (() -> ())?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
    }
    
    ///根据点击的tabBar更新对应控制器的访客界面
    func setupVistorView(imageName: String, titleString: String) {
        
        homeView.hidden = true
        
        iconView.image = UIImage(named: imageName)
        
        titleLabel.text = titleString
        
        sendSubviewToBack(coverView)
    }
    
    ///添加UI
    func prepareUI() {
        
        addSubview(iconView)
        
        addSubview(coverView)
        
        addSubview(homeView)
        
        addSubview(titleLabel)
        
        addSubview(loginBtn)
        
        addSubview(registerBtn)
        
        homeView.translatesAutoresizingMaskIntoConstraints = false
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        
        coverView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint()
    }
    
    //MARK: - 约束
    ///添加访客视图控件约束
    func addConstraint() {
        
        //转轮约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //房子
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //title
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: titleLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: titleLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))

        //登陆按钮
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: titleLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //遮盖层
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -77))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))

        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 225))
    }
    
    ///创建转轮旋转动画
    func setIconViewRotate() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        
        animation.repeatCount = MAXFLOAT
        
        animation.duration = 18
        
        animation.removedOnCompletion = false
        
        iconView.layer.addAnimation(animation, forKey: nil)
    }
    
    //MARK: - 懒加载
    
    ///房子
    lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    ///转轮
    lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    ///文本
    lazy var titleLabel: UILabel = {
        
        let title = UILabel()
        
        title.text = "关注一些人,看看有上面惊喜"
        
        title.textAlignment = NSTextAlignment.Center
        
        title.font = UIFont.systemFontOfSize(16)
        
        title.numberOfLines = 0
        
        return title
    }()
    
    ///登陆按钮
    lazy var loginBtn: UIButton = {
        
        let login = UIButton()
        
        login.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        login.setTitle("登陆", forState: UIControlState.Normal)
        
        login.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        login.addTarget(self, action: "loginBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return login
    }()
    
    ///注册按钮
    lazy var registerBtn: UIButton = {
        
        let register = UIButton()
        
        register.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        register.setTitle("注册", forState: UIControlState.Normal)
        
        register.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        register.addTarget(self, action: "registerBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return register
    }()
    
    ///遮盖view
    lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //按钮点击事件
    func loginBtnDidClick() {
        
//        print(__FUNCTION__)
        
        //闭包
//        loginBtnDidClickClosure?()
        
        //代理方法
        vistorViewDelegate!.loginBtnDidClick()
    }
    
    func registerBtnDidClick() {
        
//        print(__FUNCTION__)
        
        //闭包
//        registerDidClickClosure?()
        
        //代理方法
        vistorViewDelegate?.registerBtnDidClick()
    }
}















